First things first: why create flowcharts in R?

Although it may seem as an unnecessary and time-consuming exercise, I would argue that you could create flowcharts in less time and with way better using the diagrammeR-package results than using powerpoint. This is mainly because you don’t have to spend time with details, such as aligning tiny arrows. The purpose of this short tutorial is to share what is needed to create a nice and customizable flowchart in 5 minutes.

The package we’re going to use is DiagrammeR by Richard Iannone. However, being far from an “R-expert”, even the documentation of the diagrammeR-package may seem too complex for any one just wishing to create a PRISMA or CONSORT diagram. The good thing is that once you find the code you need, tweaking your flowcharts is a breeze. In fact, the syntax of the DiagrammeR-package is so clear that just looking at the below sample code should be enough to get a feel of how to use it. For example, consider the below code which creates a CONSORT-style flowchart:

library(DiagrammeR)

grViz("digraph {
 
#===================     
# General properties
#===================

splines=ortho 
Node [shape = box, fontname = Helvetica, fixedsize = true, width=2]

#=============================
# Create the nodes (the boxes)
#=============================

Assesed [label='Assesed for eligibility', width=3]
Randomized [label='Eligible for randomization', width=3]

A1 [label='This group']
A2 [label='Lost to follow-up']
A3 [label='Analyzed']

B1 [label='That group']
B2 [label='Lost to follow-up']
B3 [label='Analyzed']

C1 [label='Excluded']

#=============================
# Connect the nodes with lines
#=============================

Assesed -> C1 [minlen=0]
Assesed->Randomized-> {A1 B1} 
A1->A2->A3
B1->B2->B3 


}")

By looking at the above code, it is clear that: * Adding a node is as simple as just writing what it should be called (e.g. A1) * Element attributes can be set by enclosing the attributes in brackets * Nodes can be connected using “->”

Once you have entered the text you want to be displayed in each box, all that is left is to export the flowchart to an image-file. Ideally, you would export in a vector-graphics format like PDF, which allows for resizing the image without corrupting it. However, here is where it may get tricky. The DiagrammeR package does not use the regular graphics device used in R. Instead, it prints the flowchart as HTML, which hinders you from just saving it as a PDF.

Here is some potential solutions which have worked atleast once for someone: Right-clicking the image and select “Open Frame in New Window”, then click the print-icon and choose “Save as PDF”. (Only works on mac for me) Export -> Save as image - works sometimes, but doesnt generate vector-graphic Take a screenshot and paste into an image-editor. Doesnt generate vector-graphic though. Supposedly you can save it as a webpage from the Export menu, and save it from there. When i try it I get an error with the markdown-package though. I mention this because it seems to work for some others.